﻿/*
* HexStrToDec.cs
*
* 功 能： 将十六进制字符串转换成十进制数
* 类 名： HexStrToDec
*
* Ver    变更日期  20130724   负责人  黄永生  变更内容
* ───────────────────────────────────

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DQYT.HTDZ.Common
{
   public  class HexStrToDec
    {
       public HexStrToDec() 
       { }
       /// <summary>
       /// 将十六进制字符串转换成十进制数
       /// </summary>
       /// <param name="hexStr">十六进制字符串</param>
       /// <returns>十进制数</returns>
       public  float  HexStr2Dec(string hexStr)
        {   
            char[] hexCharList = hexStr.ToCharArray();
            float result = 0;
            for (int i = 0; i < hexCharList.Length; i++)
            {
                result += HexChar2Dec(hexCharList[hexCharList.Length - 1 - i]) * (float)System.Math.Pow(16, i);
            }
            return result;
        }
       /// <summary>
       /// 十六进制字符转换成数字
       /// </summary>
       /// <param name="hexChar">十六进制字符</param>
       /// <returns>整型数字</returns>
       private static float HexChar2Dec(char hexChar)
       {  
           switch (hexChar)
           {
               case '0': return 0; break;
               case '1': return 1; break;
               case '2': return 2; break;
               case '3': return 3; break;
               case '4': return 4; break;
               case '5': return 5; break;
               case '6': return 6; break;
               case '7': return 7; break;
               case '8': return 8; break;
               case '9': return 9; break;

               case 'A': return 10; break;
               case 'B': return 11; break;
               case 'C': return 12; break;
               case 'D': return 13; break;
               case 'E': return 14; break;
               case 'F': return 15; break;
               default:
                   throw new Exception("该字符不是十六进制字符");
           }
       }
   
    }
}
